﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class TimeLines : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {

        string PttID = "1";
        if (Request.Cookies["PttID"] != null)
        {
            PttID = Request.Cookies["PttID"].Value;
        }

        string strConn = ConfigurationManager.ConnectionStrings["MedicareConnectionString"].ConnectionString;
        string Block = "";   //時間軸結點標籤
        List<Recordevent> Recordlist = new List<Recordevent>();
        using (SqlConnection Conn = new SqlConnection(strConn))
        {
            string strCmd = "select timeline_Date,timeline_title,timeline_Des,TimeLine_Bytes,TimeLine_ID  from TimeLines WHERE [Ptt_ID] = @Ptt_ID order by timeline_Date asc";
            Conn.Open();
            using (SqlCommand Cmd = new SqlCommand(strCmd, Conn))
            {
                Cmd.Parameters.AddWithValue("@Ptt_ID", PttID);
                SqlDataReader dr = Cmd.ExecuteReader();
                while (dr.Read())
                {
                    string monthday = string.Format("{0}", ((DateTime)dr[0]).ToString("MM/dd"));
                    Recordlist.Add(new Recordevent(((DateTime)dr[0]).Year.ToString(), monthday, dr[1].ToString(), dr[2].ToString(), dr[3].ToString(), (int)dr[4]));
                }
                dr.Close();
                dr.Dispose();
            }
        }

        string py = "";
        string PhotoError = ""; //判斷是否有圖片;
        foreach (var item in Recordlist)
        {
            if (item.Year != py)      //判斷是否為同一年
            {
                Block += "<div class='step year'>" +
                          "<div class='dsk-titlenode'>" + item.Year + "</div>" +
                            "<div class='dsk-content'>" +
                            "<p class='dsk-year-info'></p>" +
                            "</div></div>";
                py = item.Year;              //將新的一年記起來以利判斷
            }
            if (item.PhotoName != "")
            {
                PhotoError = "<img src='HandlerTimeImg.ashx?timelineID=" + item.TimeLineID + "' alt='' /></div></div>";
            }
            else
            {
                PhotoError = "<img src='TiemlineJquery/wecare-02.png' alt='' /></div></div>";
            }
            Block += "<div class='step'>" +
                "<div class='dsk-circle'>" + item.MonthDay + "</div>" +
                "<h2 class='dsk-circle-title'>" + item.Title + "</h2>" +
                "<div class='dsk-content'>" +
                "<div class='dsk-info'>" +
                    "<h2 class='timeh2'>" + item.Title + " </h2>" +
                    "<p>" + item.Description + "</p>" +
                "<a href='../TimeLinesEdit.aspx' class='dsk-link'>編輯</a> </div>" + PhotoError;
        }
        Literal1.Text = Block;
     //   "<a href='../TimeLinesEdit.aspx?ID=" + PttID + "' class='dsk-link'>編輯</a> </div>" + PhotoError;
    }
    public class Recordevent
    {
        public Recordevent(string Years,string MonthDays, string Titles, string Descrip, string Photo, int timeid)
        {
            this.Year = Years;
            this.MonthDay = MonthDays;
            this.Title = Titles;
            this.Description = Descrip;
            this.PhotoName = Photo;
            this.TimeLineID = timeid;
        }
        public string Year { get; set; }
        public string MonthDay { get; set; }
        public string Title { get; set; }
        public string Description { get; set; }
        public string PhotoName { get; set; }
        public int TimeLineID { get; set; }
    }
}